/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import imageviewerdb.ImageDb;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;

/**
 *
 * @author Daniel
 */
public class DataReader {
    private File pathName;
    private ImageFilter filter;
    private File[] imgfiles;
    private ArrayList<ImageDb> images;
    private int numFiles;

    public DataReader(File pathName) {
        this.pathName = pathName;
        filter = new ImageFilter();
    }
    
    public ArrayList<ImageDb> read()
    {        
        imgfiles = pathName.listFiles(filter);     
        images = new ArrayList<ImageDb>(imgfiles.length);
        for (int i = 0; i< imgfiles.length; i++)
        {
            String extension = getExtension(imgfiles[i]);            
            images.add(new ImageDb(imgfiles[i].getPath(),"general","medium", extension,"No description", imgfiles[i].getName(), false));
        }
        numFiles = imgfiles.length;
        
        return images;
    }
        
    public int getNumFiles() {
        return numFiles;
    }        
    
    private String getExtension(File path) {
        String ext = null;
        String s = path.getName();
        int i = s.lastIndexOf('.');
        
        
        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }        
        return ext;
    }
    
    //devuelve el tamaño de la imagen (en pixeles)
    private String getSize(File file)
    {
    	BufferedImage temp = null;
    	try {
            temp = ImageIO.read(file);
        } catch(IOException e) {            
            e.printStackTrace();
	}
	
        if(temp == null)
            return "No size";
	else{
            return temp.getWidth() + "x" + temp.getHeight();
	}			
    }
}
